import 'package:flutter/material.dart';
import 'package:qunaimedical/components/custom_avatar.dart';
import 'package:qunaimedical/models/qa_doctor.dart';
import 'package:qunaimedical/pages/web_page.dart';
import 'package:qunaimedical/utils/navigator_util.dart';
import 'package:qunaimedical/values/urls.dart';

extension StringExtension on String {
  String maskedMiddle4Digits() {
    if (this.length > 7) {
      return this.replaceRange(3, 7, "****");
    } else {
      return this;
    }
  }
}

class QAItemDoctor extends StatelessWidget {
  final QADoctor item;
  final GestureTapCallback? onTap;

  const QAItemDoctor(this.item, {Key? key, this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap ?? () => _toDoctorDetailPage(context),
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.only(right: 16, bottom: 16),
        child: Row(
          children: [
            CustomAvatar(
              size: 60,
              margin: EdgeInsets.only(left: 16, top: 5, right: 16, bottom: 8),
              avatarUrl: item.photo,
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Container(
                        constraints: BoxConstraints(minWidth: 70),
                        child: Text(
                          item.name,
                          softWrap: false,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8),
                        child: Text(item.phone.maskedMiddle4Digits()),
                      ),
                    ],
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 4.0),
                    child: Text(item.companyId),
                  ),
                  Container(
                    padding: const EdgeInsets.symmetric(vertical: 8),
                    child: Row(
                      children: [
                        Text(
                          item.officeId,
                          style: Theme.of(context).textTheme.bodySmall,
                        ),
                        SizedBox(width: 16),
                        Text(
                          item.level,
                          style: Theme.of(context).textTheme.bodySmall,
                        )
                      ],
                    ),
                  ),
                  Divider(),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  void _toDoctorDetailPage(BuildContext context) async {
    String targetUrl = "${Urls.DOCTOR_INFO}?id=${item.id}&from=app";
    navigateTo(
      context,
      WebPage(url: targetUrl, title: "医生详情"),
    );
  }
}
